// Registration thread
import std.concurrency, std.stdio, std.socket;

import Definitions.procNames;

class CRegistry {
    __gshared Tid[E_PROC] TidRegistry;

    static void register(E_PROC name, Tid tid) {

        synchronized(this.classinfo) {
            //writeln(TidRegistry);
            TidRegistry[name] = tid;
        }
    }

    static Tid getTid(E_PROC name) {

        synchronized(this.classinfo) {
            //writeln("Entries: ", TidRegistry);
            if(name in TidRegistry) {
                //writeln ("Returning ,", TidRegistry[name], " for " , name);
                return TidRegistry[name];
            }
            return thisTid();
        }
    }
}

class CSockets {
    __gshared Socket[] SocketRegistry;

    static void register(Socket sock) {

        synchronized(this.classinfo) {
            //writeln(TidRegistry);
            SocketRegistry = SocketRegistry ~ sock;
        }
    }

    static Socket[] getSockets() {

        synchronized(this.classinfo) {
            return SocketRegistry.dup;
        }
    }

    static void remove(Socket sock) {

        Socket[] acc;
        synchronized(this.classinfo) {
            foreach(entry; SocketRegistry) {
                if(entry != sock) {
                    acc = acc ~ entry;
                }
            }
            SocketRegistry = acc.dup;
        }
    }
}
